const router = require("express").Router();
const mysql = require("./../mysql");
const User = require("./../mysql/collection/User");
const uuid = require("uuid");

/**
 * @api {get} /admin/user/list 用户列表数据
 * @apiName AdminUserList
 * @apiGroup User
 * @apiHeader {String} token  token
 * @apiParam {Number} [page=1] 当前页码
 * @apiParam {Number} [pageSize=10] 每页数量
 * @apiSuccessExample {json} Success-Response:
 *  {
 *    code: '200',
 *    message: '用户列表数据',
 *    data: {
 *      list,
 *      total,
 *      page: Number(page),
 *      pageSize: Number(pageSize)
 *    }
 *  }
 */
router.get("/list", (req, res, next) => {
  const { page = 1, pageSize = 10 } = req.query;
  const skip = (page - 1) * pageSize;
  Promise.all([
    mysql.find(User, {}, { skip: Number(skip), limit: Number(pageSize),__v: 0 }),
    mysql.count(User, {})
  ]).then(([list, total]) => {
    res.send({
      code: "200",
      message: "用户列表数据",
      data: {
        list,
        total,
        page: Number(page),
        pageSize: Number(pageSize)
      }
    });
  });
});

module.exports = router;
